package com.qf.service.impl;

import com.qf.pojo.Stu;
import com.qf.service.StuService;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.awt.print.Book;
import java.sql.SQLException;
import java.util.List;

public class StuServiceImpl implements StuService {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public Stu login(String stuname, String pwd) {
        String sql=" SELECT * FROM `stuinfo` WHERE `stuname`=? AND `pwd`=? ";
        try {
            return queryRunner.query(sql ,new BeanHandler<>(Stu.class),stuname,pwd);
        } catch (SQLException e) {
            throw  new RuntimeException(e);
        }
    }

    @Override
    public void insertStu(Stu stu) {
        String sql=" INSERT INTO `stuinfo` (`stuname`,`stuno`,`pwd`,`idcard`,`startdate`,`status`) VALUES(?,?,?,?,?,?) ";
        Object[] params={stu.getStuname(),stu.getStuno(),stu.getPwd(),stu.getIdcard(),stu.getStartdate(),stu.getStatus()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Stu> selectAll(int page_num, int page_size) {
        String sql=" SELECT `id`,`stuname`,`stuno`,`pwd`,`idcard`,`startdate`,`STATUS` FROM `stuinfo` ORDER BY id LIMIT ?,? ";
        try {
            //return queryRunner.query(sql,new BeanListHandler<>(Stu.class));
            return  queryRunner.query(sql,new BeanListHandler<>(Stu.class),(page_num-1)*page_size,page_size);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(int id) {
        String sql=" DELETE FROM `stuinfo` WHERE `id`=? ";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(Stu stu) {
        String sql=" UPDATE `stuinfo` SET `stuname`=?,`stuno`=?,`pwd`=?,`idcard`=?,`startdate`=?,`status`=? WHERE `id`=? ";
        Object[] params={stu.getStuname(),stu.getStuno(),stu.getPwd(),stu.getIdcard(),stu.getStartdate(),stu.getStatus(),stu.getId()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long selectCount() {
        String sql=" SELECT COUNT(*) FROM `stuinfo`; ";
        try {
            return queryRunner.query(sql,new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Stu> selectByPage(int page_num, int page_size) {
        String sql=" SELECT * FROM `stuinfo` ORDER BY id LIMIT ?,?  ";;
        try {
            return queryRunner.query(sql,new BeanListHandler<>(Stu.class),(page_num-1)*page_size,page_size);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
